package com.common.domain;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;

/**
 * @author 大静
 * @version 1.0
 * @date 2021-05-07 15:56
 * @description
 */
@Data
@Document(indexName = "supermarket",type = "Product")
public class Product implements Serializable {
  @Id private String productId;

  @Field(type = FieldType.Text, analyzer = "ik_max_word")
  private String productName;

  @Field(type = FieldType.Double)
  private Double productPrice;

  @Field(type = FieldType.Text, analyzer = "ik_max_word")
  private String productCategory;

  @Field(type = FieldType.Keyword,index = false)
  private String productImageurl;

  @Field(type = FieldType.Integer)
  private Integer productNum;

   @Field(type = FieldType.Text, analyzer = "ik_max_word")
  private String productDescription;

  @Override
  public String toString() {
    try {
      return new ObjectMapper().writeValueAsString(this);
    } catch (JsonProcessingException e) {
      e.printStackTrace();
      return "{" +
              "\"productId\": \"" + this.productId + "\"," +
              "\"productName\": \"" + this.productName + "\"," +
              "\"productPrice\": " + this.productPrice + "," +
              "\"productCategory\": \"" + this.productCategory + "\"," +
              "\"productImgurl\": \"" + this.productImageurl + "\"," +
              "\"productNum\": " + this.productNum + "," +
              "\"productDescription\": \"" + this.productDescription + "\"," +
              "}";
    }
  }
}
